import { get, post, put, del } from '@/utils/request';
import type { DictionaryType, DictionaryItem } from '@/types';
import type {
  GetDictionaryTypesParams,
  CreateDictionaryTypeData,
  UpdateDictionaryTypeData,
  GetDictionaryItemsParams,
  CreateDictionaryItemData,
  UpdateDictionaryItemData,
} from './type';

/**
 * 字典管理 API
 */

// ============ 字典类型 ============

export const getDictionaryTypes = (params: GetDictionaryTypesParams) => {
  return get<any>('/dictionaries/types', params as any);
};

export const getDictionaryTypeDetail = (id: string) => {
  return get<DictionaryType>(`/dictionaries/types/${id}`);
};

export const createDictionaryType = (data: CreateDictionaryTypeData) => {
  return post<DictionaryType>('/dictionaries/types', data);
};

export const updateDictionaryType = (id: string, data: UpdateDictionaryTypeData) => {
  return put<DictionaryType>(`/dictionaries/types/${id}`, data);
};

export const deleteDictionaryType = (id: string) => {
  return del(`/dictionaries/types/${id}`);
};

// ============ 字典项 ============

export const getDictionaryItems = (params: GetDictionaryItemsParams) => {
  return get<any>('/dictionaries/items', params as any);
};

export const createDictionaryItem = (data: CreateDictionaryItemData) => {
  return post<DictionaryItem>('/dictionaries/items', data);
};

export const updateDictionaryItem = (id: string, data: UpdateDictionaryItemData) => {
  return put<DictionaryItem>(`/dictionaries/items/${id}`, data);
};

export const deleteDictionaryItem = (id: string) => {
  return del(`/dictionaries/items/${id}`);
};

// 根据编码获取字典项
export const getDictionaryItemsByCode = (code: string) => {
  return get<DictionaryItem[]>(`/dictionaries/code/${code}`);
};

